package com.ym.b2b.user.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.ym.b2b.commons.conf.WebTokenUser;
import com.ym.b2b.commons.controller.BaseController;
import com.ym.b2b.commons.log.CusLog;
import com.ym.b2b.user.service.EmCUserService;
import com.ym.b2b.user.sqlbean.EmCUser;
import com.zc.commons.base.Page;
import com.zc.commons.base.Result;
import com.zc.export.ExcelUtil;

@Controller
@RequestMapping("/user")
public class ManagerEmCUserController extends BaseController {

    @Autowired
    private EmCUserService emCUserService;

    /**
     * 获取用户分页信息
     */
    @RequestMapping(value = "searchPageUser.do")
    @ResponseBody
    public Result searchPageUser(EmCUser emCuser, Page<EmCUser> page, HttpServletRequest req) {
        Result ret = new Result();
        try {
            emCUserService.searchPageEmCUser(emCuser, page);
            ret.setResultPage(page);
        } catch (Exception e) {
            ret.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return ret;
    }

    // 列表查询
    @RequestMapping("searchPageEmCUser.do")
    @ResponseBody
    public Result searchPageEmCUser(EmCUser emCuser, Page<EmCUser> page, HttpServletRequest req) {
        Result result = new Result();
        result = emCUserService.searchPageEmCUser(emCuser, page);
        return result;
    }

    @RequestMapping(value = "searchPageEmCUserExcel.do")
    @ResponseBody
    public Result searchPageEmCUserExcel(EmCUser emCuser, Page<EmCUser> page, String loadType) {
        Result ret = new Result();
        try {
            if (loadType == null || loadType.equals("")) {
                ret.setCode(Result.CODE_100);
                ret.setResult("loadType 参数 '数量'  不能为空!");
                return ret;
            }
            page.setPageable("true");
            page.setPageSize(Integer.parseInt(loadType));
            page.setStartRow(1);
            ret = emCUserService.searchPageEmCUser(emCuser, page);
            ExcelUtil<EmCUser> excelUtil = new ExcelUtil<EmCUser>();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("model", ret.getRows());
            ret = excelUtil.createExcel("userListExcel", map);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            ret.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return ret;
    }

    // 获取单条记录内容
    @RequestMapping("searchEmCUser.do")
    @ResponseBody
    public EmCUser searchEmCUser(EmCUser emCuser, HttpServletRequest req) {
        EmCUser result = new EmCUser();
        result = emCUserService.searchEmCUser(emCuser);
        return result;
    }

    // 更新记录
    @RequestMapping("updateEmCUser.do")
    @ResponseBody
    public Result updateEmBNews(EmCUser emCuser, HttpServletRequest req) throws DataAccessException {
    	CusLog.writeLog(getManagerTokenUser(req).getUserName() + " 更新用户信息,各字段值："+ JSON.toJSONString(emCuser), "USER", 2);
        Result result = new Result();
        result = emCUserService.updateEmCUser(emCuser);
        return result;
    }

    // 批量更新记录
    @RequestMapping("updateEmCUserBat.do")
    @ResponseBody
    public Result updateEmBNewsBat(EmCUser emCuser, HttpServletRequest req) throws DataAccessException {
    	CusLog.writeLog(getManagerTokenUser(req).getUserName() + " 批量更新用户信息,各字段值："+ JSON.toJSONString(emCuser), "USER", 2);
        Result result = new Result();
        result = emCUserService.updateEmCUserBat(emCuser);
        return result;
    }

    // 删除记录内容
    @RequestMapping("deleteEmCUser.do")
    @ResponseBody
    public Result deleteEmCUser(EmCUser emCuser, HttpServletRequest req) {
    	CusLog.writeLog(getManagerTokenUser(req).getUserName() + " 删除用户信息,各字段值："+ JSON.toJSONString(emCuser), "USER", 2);
        Result result = new Result();
        try {
            emCUserService.deleteEmCUser(emCuser.getUserId());
        } catch (Exception e) {
            // TODO: handle exception
            result.setCode("100");
            return result;
        }
        result.setCode("200");
        return result;
    }

    // 删除多条记录内容
    @RequestMapping("deleteMultipleEmCUser.do")
    @ResponseBody
    public Result deleteMultipleEmCUser(String userIdList, HttpServletRequest req) {
    	CusLog.writeLog(getManagerTokenUser(req).getUserName() + " 删除多条用户信息,各字段值："+ JSON.toJSONString(userIdList), "USER", 2);
        Result result = new Result();
        try {
            emCUserService.deleteMultipleEmCUser(userIdList);
        } catch (Exception e) {
            // TODO: handle exception
            result.setCode("100");
            return result;
        }
        result.setCode("200");
        return result;
    }

    /**
     * @Title: updateUserInformation
     * @Description: 修改
     */
    @ResponseBody
    @RequestMapping(value = "updateUserInformation.do")
    public Result updateUserInformation(EmCUser emCuser, HttpServletRequest req) {
    	CusLog.writeLog(getManagerTokenUser(req).getUserName() + " 修改用户信息,各字段值："+ JSON.toJSONString(emCuser), "USER", 2);
        Result ret = new Result();
        try {
            Date dt = new Date();
            emCuser.setUpdateDate(dt);
            WebTokenUser wtu = getWebTokenUser(req);
            emCuser.setUserId(Long.parseLong(String.valueOf(wtu.getUserId())));
            ret = emCUserService.update(emCuser);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }
        return ret;
    }

    @RequestMapping(value = "addProviderUser.do")
    @ResponseBody
    public Result addProviderUser(EmCUser emCUser, HttpServletRequest req) {
    	CusLog.writeLog(getManagerTokenUser(req).getUserName() + " 新增用户信息,各字段值："+ JSON.toJSONString(emCUser), "USER", 2);
        Result result = new Result();
        try {
            result = emCUserService.insertUser(emCUser);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }
}
